package com.lanou.tipsy.adapter.mv;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.mv.MvListEntity;

import java.util.ArrayList;

/**
 * Created by wangshuai on 15/10/30.
 *
 */
public class MvAdapter extends BaseAdapter {


    private Context mContext;
    private ArrayList<MvListEntity> mvListEntities;

    //构造方法
    public MvAdapter(Context mContext, ArrayList<MvListEntity> mvListEntities) {
        this.mContext = mContext;
        this.mvListEntities = mvListEntities;

    }

    //适配器数据刷新的方法
    public void refreshData(ArrayList<MvListEntity> mvListEntities) {

        this.mvListEntities = mvListEntities;

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mvListEntities != null && mvListEntities.size() > 0) {
            return mvListEntities.size();
        } else

            return 0;
    }

    @Override
    public MvListEntity getItem(int position) {

        if (mvListEntities != null && mvListEntities.size() > 0) {

            return mvListEntities.get(position);
        } else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.gridview_music_mv, null);
            //初始化
            holder = new ViewHolder();

            holder.musicMvGV = (SimpleDraweeView) convertView.findViewById(R.id.musicMvGV);
            holder.mvNameTv = (TextView) convertView.findViewById(R.id.mvNameTv);
            holder.musicNameTv = (TextView) convertView.findViewById(R.id.musicNameTv);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        MvListEntity m = getItem(position);
        if (m != null) {

            Uri uri = Uri.parse(m.getThumbnail());
            holder.musicMvGV.setImageURI(uri);
            holder.mvNameTv.setText(m.getTitle());
            holder.musicNameTv.setText(m.getArtist());

        }
        return convertView;
    }

    private class ViewHolder {
        private SimpleDraweeView musicMvGV; // mv图片
        private TextView mvNameTv;  //mv名字
        private TextView musicNameTv;//音乐名字

    }

}
